package org.msfj.toilets.data.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.msfj.toilets.data.mapper.HumitureInfoMapper;
import org.msfj.toilets.data.service.HumitureInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.msfj.toilets.model.HumitureInfo;
import org.msfj.toilets.vo.HumitureInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 系统温湿度信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "humitureInfo")
public class HumitureInfoServiceImpl extends BaseServiceImpl<HumitureInfo> implements HumitureInfoService {

    @Autowired
    HumitureInfoMapper humitureInfoMapper;

    /**
     * 根据条件分页查询系统温湿度信息列表
     *
     * @param humitureInfoVo
     * @param page           当前第几页
     * @param rows           每页多少条记录
     * @return PageInfo<HumitureInfo>
     */
    @Override
    public PageInfo<HumitureInfoVo> getByHumitureInfo(HumitureInfoVo humitureInfoVo, int page, int rows) {
        Example example = new Example(HumitureInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        PageHelper.startPage(page, rows);
        List<HumitureInfo> humitureInfoList = super.getByExample(example);
        PageInfo humitureInfoVoPageInfo = new PageInfo(humitureInfoList);
        humitureInfoVoPageInfo.setList(setOtherProperties(humitureInfoList));
        return humitureInfoVoPageInfo;
    }

    /**
     * 根据条件查询所有系统温湿度信息对象列表
     *
     * @param humitureInfoVo
     * @return List<HumitureInfo>
     */
    @Override
    public List<HumitureInfoVo> getAllHumitureInfo(HumitureInfoVo humitureInfoVo) {
        Example example = new Example(HumitureInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        if (ObjectUtil.isNotNull(humitureInfoVo.getCreateTime())) {
            String format = DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN);
            criteria.andBetween("create_time",DateUtil.parse(format + " 00:00:00").toJdkDate(), DateUtil.parse(format + " 23:59:59").toJdkDate());
        }
        example.setOrderByClause("sort_no asc");
        List<HumitureInfoVo> humitureInfoVoList = setOtherProperties(super.getByExample(example));
        return humitureInfoVoList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param humitureInfo
     * @return DepartmentVo
     */
    @Override
    public HumitureInfoVo setOtherProperties(HumitureInfo humitureInfo) {
        HumitureInfoVo humitureInfoVo = new HumitureInfoVo();
        BeanUtils.copyProperties(humitureInfo, humitureInfoVo);
        return humitureInfoVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param humitureInfos
     * @return List<HumitureInfoVo>
     */
    @Override
    public List<HumitureInfoVo> setOtherProperties(List<HumitureInfo> humitureInfos) {
        List<HumitureInfoVo> humitureInfoVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(humitureInfos)) {
            for (HumitureInfo humitureInfo : humitureInfos) {
                HumitureInfoVo humitureInfoVo = new HumitureInfoVo();
                BeanUtils.copyProperties(humitureInfo, humitureInfoVo);
                humitureInfoVos.add(humitureInfoVo);
            }
        }
        return humitureInfoVos;
    }
}
